package com.zxbit.flash.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.zxbit.flash.R;
import com.zxbit.flash.adapter.FormAdapter;
import com.zxbit.flash.common.DialogManager;

public class FormDialog extends Dialog implements View.OnClickListener {

    private ImageView ivClose;
    private RecyclerView recycler;
    private TextView btnConfirm;

    private Context context;
    private FormAdapter adapter;
    private FormClickListener listener;

    public FormDialog(@NonNull Context context, String[] key, String[] value) {
        super(context);
        init(context, key, value);
    }

    public FormDialog(@NonNull Context context, int themeResId, String[] key, String[] value) {
        super(context, themeResId);
        init(context, key, value);
    }

    protected FormDialog(@NonNull Context context, boolean cancelable, @Nullable OnCancelListener cancelListener,
                         String[] key, String[] value) {
        super(context, cancelable, cancelListener);
        init(context, key, value);
    }

    private void init(Context context, String[] key, String[] value) {
        this.context = context;
        View rootView = View.inflate(context, R.layout.layout_form_dialog, null);
        ivClose = rootView.findViewById(R.id.iv_close);
        recycler = rootView.findViewById(R.id.recycler);
        btnConfirm = rootView.findViewById(R.id.btn_confirm);
        setContentView(rootView);

        recycler.setLayoutManager(new LinearLayoutManager(context));
        DividerItemDecoration divider = new DividerItemDecoration(context, DividerItemDecoration
                .VERTICAL);
        divider.setDrawable(ContextCompat.getDrawable(context, R.drawable.custom_divider));
        recycler.addItemDecoration(divider);
        adapter = new FormAdapter(context, key, value);
        recycler.setAdapter(adapter);

        Window window = getWindow();
        assert window != null;
        WindowManager.LayoutParams params = window.getAttributes();
        params.gravity = Gravity.CENTER;
        // 设置对话框大小
        params.width = context.getResources().getDimensionPixelOffset(R.dimen.dialog_width);
        window.setAttributes(params);

        setCancelable(false);
        setCanceledOnTouchOutside(false);
        ivClose.setOnClickListener(this);
        btnConfirm.setOnClickListener(this);
        rootView.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_close:
                DialogManager.getManager().dismissDialog(this);
                break;
            case R.id.btn_confirm:
                if (listener != null) {
                    listener.onConfirmClick();
                } else {
                    DialogManager.getManager().dismissDialog(this);
                }
                break;
            default:
                //隐藏输入法
                InputMethodManager manager = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE);
                manager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), InputMethodManager.HIDE_NOT_ALWAYS);
                break;
        }
    }

    public String[] getForm() {
        return adapter.getValue();
    }

    public void setOnClickListener(FormClickListener listener) {
        this.listener = listener;
    }

    public interface FormClickListener {
        void onConfirmClick();
    }

}
